package com.ophiux.FeverManagement.infratructure.repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentDetail;
import com.ophiux.FeverManagement.domain.entity.RbTreatment;
import com.ophiux.FeverManagement.domain.entity.RbTreatmentMedicine;
import com.ophiux.FeverManagement.domain.repository.ITreatmentRepository;
import com.ophiux.FeverManagement.infratructure.mapper.RbTreatmentMapper;
import com.ophiux.FeverManagement.infratructure.mapper.RbTreatmentMedicineMapper;
import com.ophiux.utils.exception.OphiuxDaoException;
import com.ophiux.utils.exception.OphiuxRepositoryException;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Repository
public class TreatmentRepository implements ITreatmentRepository {
	@Autowired
	RbTreatmentMapper treatmentMapper;
	@Autowired
	RbTreatmentMedicineMapper treatmentMedicineMapper;
	
	
	@Override
	public String save(RbTreatment model) throws OphiuxRepositoryException {
		
		return null;
	}

	@Override
	public void delete(Object uniqueId) throws OphiuxRepositoryException {
		
		
	}

	@Override
	public RbTreatment selectOneByKey(Object uniqueId) throws OphiuxRepositoryException {
		try {
			//uniqueId==TREATMENT_ID
			RbTreatment model = treatmentMapper.selectOneByKey(uniqueId.toString());
			return model;
		} catch (OphiuxDaoException e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

	@Override
	public RbTreatment selectOne(Map map) throws OphiuxRepositoryException {
		
		return null;
	}

	@Override
	public List<RbTreatment> selectAll(Map map) throws OphiuxRepositoryException {
		try {
			List<RbTreatment> list = treatmentMapper.selectAll(map);
			for (RbTreatment rbTreatment : list) {
				Map<String,String> detailMap = new HashMap<String,String>();
				detailMap.put("TREATMENT_ID", rbTreatment.getTREATMENT_ID());
				List<RbTreatmentMedicine> detailList = treatmentMedicineMapper.selectAll(detailMap);
				rbTreatment.setMEDICINE(detailList);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据库执行异常,异常信息", e);
			throw new OphiuxRepositoryException(e);
		}
	}

}
